package com.ht.medicaltreatment.activity

import android.content.Intent
import android.view.View
import androidx.activity.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.LogUtils
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.listener.OnItemClickListener
import com.gym.baselib.base.BaseActivity
import com.ht.medicaltreatment.R
import com.ht.medicaltreatment.adapter.FromRecordAdaptger
import com.ht.medicaltreatment.bean.BloodOrderDetailBean
import com.ht.medicaltreatment.bean.BloodsBean
import com.ht.medicaltreatment.bean.Drawblood
import com.ht.medicaltreatment.viewmodel.BloodOrderListViewModel
import com.xgst.lib.utils.showToast
import kotlinx.android.synthetic.main.activity_form_record_layout.*

/**
 * 表单记录
 */
class FormRecordActivity:BaseActivity(), OnItemClickListener {
    private val model by viewModels<BloodOrderListViewModel>()
    private var adapter:FromRecordAdaptger? = null
    override fun getLayoutId() = R.layout.activity_form_record_layout

    private var id = -1

    override fun init() {
        setTitle("表单记录")
        id = intent.getIntExtra("uid",-1)
        recyclerView.layoutManager = LinearLayoutManager(this)
    }

    override fun onResume() {
        super.onResume()
        showProgress()
        val bloodsList = arrayListOf<BloodsBean>()
        model.request("$id"){
            dismissProgress()
            if(it.drawBloodMoreOrder?.bloodNum == 1){
                it.drawbloodList.forEach { item ->
                    val list = arrayListOf<Drawblood>()
                    list.add(item)
                    bloodsList.add(BloodsBean(list))
                }
            }else{
                //多管
                val drawbloodList = it.drawbloodList
                var startIndex = 0
                //当前时间点
                while (startIndex < drawbloodList.size){
                    val list = arrayListOf<Drawblood>()
                    val bloodGroupSum = drawbloodList[startIndex].bloodGroupSum
                    //当前时间点的管
                    for(i in 0 until bloodGroupSum){
                        if(i+startIndex >= it.drawbloodList.size) return@request
                        val drawblood = it.drawbloodList[i+startIndex]
                        list.add(drawblood)
                    }
                    bloodsList.add(BloodsBean(list))
                    startIndex = startIndex + bloodGroupSum
                }
            }
            adapter = FromRecordAdaptger()
            recyclerView.adapter = adapter
            adapter!!.setOnItemClickListener(this)
            adapter!!.setNewInstance(bloodsList)
        }
    }

    override fun onItemClick(adapter1: BaseQuickAdapter<*, *>, view: View, position: Int) {
        val item = adapter1.getItem(position) as BloodsBean
        if (item.list.size <= 0){
            showToast("暂无数据")
            return
        }
        if(position > 0){
            val preBean = adapter1.getItem(position-1) as BloodsBean
            if(preBean.list != null && preBean.list.size > 0 && preBean.list[0].checkSuccess == 0){
                showToast("上一个时间点未核验")
                return
            }
        }
        val intent = Intent()
        intent.setClass(this,MultiSampleCollectionDetailActivity::class.java)
        intent.putParcelableArrayListExtra("bloodList",item.list)
        startActivity(intent)
    }

}
